<?php

namespace App\Controllers\Lessions;

use App\Models\Tag\Tags;
use \App\Libs\Views\Json;
use App\Models\Tag\TagLation;
use App\Models\Lession\Course;
use App\Models\Lession\Scolumn;
use App\Models\Lession\Lession;
//获取：家长课程列表(1)；孩子课程列表(2)；推荐试听列表(4)；垂直课程(3)
//tag_type  ：  1,2,4,3
//tag_id:  : 由app传递;
//page  :1；
//num ： 10；

//垂直课程：需要额外参数：course_type:指定获取的课程类型：1：课程，2：专栏，3：大咖;

class GetCourseList extends \App\Libs\Base\BaseAction
{

    public function execute()
    {
        if(!$this->request->hasQuery('tag_type') || !$this->request->hasQuery('tag_id')){
            return \App\Libs\Views\Json::view(1, '缺少参数', null);
        }
        $tagType = $this->request->getQuery('tag_type');
        $tagId = $this->request->getQuery('tag_id');
        $page = $this->request->getQuery('page','int',1);
        $num = $this->request->getQuery('num','int',10);
        $offset = ($page-1)*$num;
        $matchTag = Tags::findOne([
            'conditions' => 'id=?1 and tag_type=?2 and isshow=1',
            'bind' => [1=>$tagId,2=>$tagType]
        ]);
        if(!empty($matchTag)){
            switch ($matchTag->tag_type) {
                //1:家长课程，2：孩子课程，只有课程，没有专栏，大咖等，所以直接去课程表中找;
                case "1":
                case '2':
                        $matchTagArrayMode = $matchTag->toArray(['id','tag_name','tag_type']);
                        $extraData = json_decode($matchTag->data,true);
                        isset($extraData['listTitle']) ? $matchTagArrayMode['listTitle'] = $extraData['listTitle'] : $matchTagArrayMode['listTitle'] = '无数据';
                        isset($extraData['listLogo']) ? $matchTagArrayMode['listLogo'] = $extraData['listLogo'] : $matchTagArrayMode['listLogo'] = '无数据';
                        isset($extraData['listIntro']) ? $matchTagArrayMode['listIntro'] = $extraData['listIntro'] : $matchTagArrayMode['listIntro'] = '无数据';
                        $finalData['info'] = $matchTagArrayMode;

                        $allRelation = TagLation::findAll([
                            'conditions' => 'tagid=?1',
                            'offset' => $offset,
                            'limit' => $num,
                            'bind' =>[1=>$tagId]
                        ]);
                        $allRelation->rewind();
                        if($allRelation->valid()){
                            while($allRelation->valid()){
                                $curentRelation = $allRelation->current();
                                $courseIdArray[] = $curentRelation->costarid;
                                $allRelation->next();
                            }
                            $allMatchCourse = Course::findAll([
                                'conditions' => 'id in ({courseid:array}) and isshow=1',
                                'columns' =>'id as courseid,course_name,course_intro',
                                'bind' => ['courseid' => $courseIdArray]
                            ]);
                            $allMatchCourse->rewind();
                            while($allMatchCourse->valid()){
                                $finalData['list'][] = $allMatchCourse->current()->toArray();
                                $allMatchCourse->next();
                            }
                            return \App\Libs\Views\Json::view(0, '成功', $finalData);
                        }else{
                            $finalData['list'] = [];
                            return \App\Libs\Views\Json::view(1, '此集合暂未添加数据', null);
                        }
                    break;
                //垂直课程；分为专栏，课程；纯列表;
                case '3': //
                    $courseType = $this->request->getQuery('course_type','int',1);
                    $allRelation = TagLation::findAll([
                        'conditions' => 'tagid=?1 and type=?2',
                        'offset' => $offset,
                        'limit' => $num,
                        'bind' =>[1=>$tagId,2=>$courseType]
                    ]);
                    $allRelation->rewind();
                    if($allRelation->valid()){
                        while($allRelation->valid()){
                            $courseTypeIdArray[] = $allRelation->current()->costarid;
                            $allRelation->next();
                        }
                        switch ($courseType){
                            case '1' : //课程
                                $table = "App\Models\Lession\Course";
                                break;
                            case '2' :  //专栏
                                $table = 'App\Models\Lession\Scolumn';
                                break;
                        }
                        $allTypeCourse = $table::findAll([
                            'conditions' => 'id in ({cosArray:array}) and isshow=1',
                            'columns' => 'id,list_logo,title,little_intro,master,sale_price',
                            'bind' => ['cosArray' => $courseTypeIdArray]
                        ]);
                        $allTypeCourse->rewind();
                        while($allTypeCourse->valid()){
                            $currentCourse= $allTypeCourse->current();
                            $finalData[] = $currentCourse;
                            $allTypeCourse->next();
                        }
                        return \App\Libs\Views\Json::view(0, '成功', $finalData);
                    }else{
                        return \App\Libs\Views\Json::view(1, '没有数据更多', null);
                    }
                    break;
                case '4':
                        $matchTag = Tags::findOne([
                            'conditions' => 'id=?1 and tag_type=?2',
                            'bind' =>[1=>$tagId,2=>$tagType]
                        ]);

                        if(!empty($matchTag)){
                            $finalData= [];
                            //找到所有的对应的课程，;
                            $allRelation = TagLation::findAll([
                                'conditions' => 'tagid=?1 and type=1',
                                'bind' => [1=>$matchTag->id]
                            ]);
                            $allRelation->rewind();
                            if($allRelation->valid()){
                                while($allRelation->valid()){
                                    $currentRelation = $allRelation->current();
                                    $courseId[] = $currentRelation->costarid;
                                    $allRelation->next();
                                }
                                $allMatchCourses = Course::findAll([
                                    'conditions' => 'id IN ({sdfdsf:array})',
                                    'bind' => ['sdfdsf' => $courseId]
                                ]);
                                $allMatchCourses->rewind();
                                while($allMatchCourses->valid()){
                                    $currentCourse = $allMatchCourses->current();
                                    $finalData[] = array_merge($currentCourse->toArray(['id','title','little_intro','master','price']),['type' => '1','type_text' =>'course']);
                                    $allMatchCourses->next();
                                }
                            }
                            //找到所有的专栏;type=2;
                            $allRelation = TagLation::findAll([
                                'conditions' => 'tagid=?1 and type=2',
                                'bind' => [1=>$matchTag->id]
                            ]);
                            $allRelation->rewind();
                            if($allRelation->valid()){
                                while($allRelation->valid()){
                                    $currentRelation = $allRelation->current();
                                    $courseId[] = $currentRelation->costarid;
                                    $allRelation->next();
                                }
                                $allMatchCourses = Scolumn::findAll([
                                    'conditions' => 'id in ({arrayid:array})',
                                    'bind' => ['arrayid' => $courseId]
                                ]);
                                $allMatchCourses->rewind();
                                while($allMatchCourses->valid()){
                                    $currentCourse = $allMatchCourses->current();
                                    $finalData[] = array_merge($currentCourse->toArray(['id','title','little_intro','master','price']),['type' => '2','type_text' =>'scolumn']);
                                    $allMatchCourses->next();
                                }
                            }
                            if(!empty($finalData)){
                                return \App\Libs\Views\Json::view(0, '成功', $finalData);
                            }else{
                                return \App\Libs\Views\Json::view(1, '指定的数据不存在', null);
                            }
                        }else{
                            return \App\Libs\Views\Json::view(1, '指定的数据不存在', null);
                        }
                    break;
            }
        }else{
            return \App\Libs\Views\Json::view(1, '指定的数据不存在', null);
        }
    }
}
